package com.example.demo10.controller;


import com.example.demo10.util.SFTPUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(MultipartFile myFile, String username, String password, String host){
        /**
         * 连接指定的linux
         */
        System.out.println("传如到controller的信息"+myFile.toString()+":"+username+":"+password+":"+host);
        SFTPUtil sftp = new SFTPUtil(username, password, host, 22);
        sftp.login();

        //byte[] buff = sftp.download("/opt", "start.sh");
        //System.out.println(Arrays.toString(buff));
        try {
            //获取源文件的名字
            String fileName = myFile.getOriginalFilename();
            System.out.println(fileName);
            //获取原文件的后缀
            String suffixName = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();

            sftp.upload("/usr/local",fileName, myFile.getInputStream());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("上传失败");
        }finally {
            sftp.logout();
        }
        return "成功";

    }
}
